package com.bleet.securitysms;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
	private SharedPreferences prefsPrivate;
	
	private EditText mFirstPwd, mConfirmPwd;
	private Button mNext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!SettingStatus.isCreatedpwd(this)){
        	setContentView(R.layout.main);
        	setupViews();
        }else {
        	Intent intent = new Intent(this, Input.class);
			startActivity(intent);
			finish();
		}
    }

	private void setupViews() {
		mFirstPwd = (EditText) findViewById(R.id.firstpass);
		mConfirmPwd = (EditText) findViewById(R.id.confirmpass);
		mNext = (Button) findViewById(R.id.next);
		mNext.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == mNext) {
			String firstPwd = mFirstPwd.getText().toString();
			String confirmPwd = mConfirmPwd.getText().toString();
			if(firstPwd.matches("[0-9a-zA-Z]{6,8}")){
				if(confirmPwd.matches("[0-9a-zA-Z]{6,8}")){
					if (firstPwd.equals(confirmPwd)) {
						prefsPrivate = getSharedPreferences(PREFS_PRIVATE, Context.MODE_PRIVATE);
						Editor prefsPrivateEditor = prefsPrivate.edit();
						prefsPrivateEditor.putString("password", Security.toMd5(firstPwd.getBytes()));
						prefsPrivateEditor.putBoolean("createdpwd", true);
						prefsPrivateEditor.commit();
						Toast.makeText(this,getResources().getString(R.string.createpwdsuccess),Toast.LENGTH_LONG).show();
						Intent intent = new Intent(this, Input.class);
						startActivity(intent);
						finish();
					} else {
						Toast.makeText(this,getResources().getString(R.string.inputpwdagain),Toast.LENGTH_LONG).show();
					}
				}else {
					Toast.makeText(this,getResources().getString(R.string.passwordformaterror),Toast.LENGTH_LONG).show();
				}
			}else {
				Toast.makeText(this,getResources().getString(R.string.passwordformaterror),Toast.LENGTH_LONG).show();
			}
			
		}
	}
}